package cn.qn.moudle.security;

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Bryant
 * @Date: 2018/5/25
 * @Description: 用户账号表
 */
@Entity(name="security_member_account")
@Data
@org.hibernate.annotations.Table(appliesTo  = "security_member_account",comment="用户账户表")
public class MemberAccount implements UserDetails {

    private static final long serialVersionUID = 8657789589652380102L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id" ,columnDefinition="int(11) COMMENT '主键'")
    private Integer id;

    @Column(name = "user_id",columnDefinition="varchar(20) COMMENT '用户ID'")
    private String userId;

    @Column(name = "mobile",columnDefinition="varchar(20) COMMENT '绑定手机号'")
    private String mobile;

    @Column(name = "email" ,columnDefinition="varchar(50) COMMENT '邮箱'")
    private String email;

    @Column(name = "user_type",columnDefinition="int(11) COMMENT '用户类型，对应UserTypeEnum用户类型枚举'")
    private Integer userType;

    @Column(name = "user_name",columnDefinition="varchar(50) COMMENT '用户名称'")
    private String userName;

    @Column(name = "head_image",columnDefinition="varchar(50) COMMENT '头像地址'")
    private String headImage;

    @Column(name = "password",columnDefinition="varchar(64) COMMENT '密码'")
    private String password;

    @Column(name = "password_security_level",columnDefinition="int(11) default '1' COMMENT '密码安全级别:1-低，2-中，3-高'")
    private Integer passwordSecurityLevel;

    @Column(name = "status",columnDefinition="int(11) COMMENT '状态:1-正常，0-禁用'")
    private Integer status;

    @Column(name = "create_time" ,columnDefinition="timestamp COMMENT '数据创建时间'")
    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date createTime;

    @Column(name = "update_time" ,columnDefinition="timestamp COMMENT '数据创建时间'")
    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updateTime;

    @Transient
    private String Authorization;

    @Transient
    private List<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public String getPassword() {
        return password;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        if("1".equals(String.valueOf(status))){
            return true;
        }
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public String getAuthorization() {
        return Authorization;
    }

    public void setAuthorization(String authorization) {
        Authorization = authorization;
    }
}
